package com.java2207.group1.controller;

import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.common.R;
import com.java2207.group1.pojo.Hotel;
import com.java2207.group1.service.HotelService;
import com.java2207.group1.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * @author :xs
 * @date : 2022/7/26
 */
@RequestMapping("/hotel")
@RestController
public class HotelController {

    @Autowired
    private HotelService hotelService;

    //分页
    @RequestMapping("/listHotel")
    public LayuiPageVo<Hotel> listHotel(@RequestParam Map<String, Object> params){
        LayuiPageVo<Hotel> hotel=hotelService.listHotel(params);
        return hotel;
    }
    // 删除
    @RequestMapping("/deleteHotel")
    public R deleteHotel(Integer id){
        int i = hotelService.deleteHotel(id);
        if(i==1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //删除
    @GetMapping("/deleteHotelsByIds")
    public R deleteHotelsByIds(String ids){
        int result = hotelService.deleteHotelsByIds(ids);
        if (result>0){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //添加
    @RequestMapping("/addHotel")
    public  R addHotel(Hotel hotel){
        int i = hotelService.addHotel(hotel);
        if(i==1){
            return R.ok();
        }else {
            return R.error();
        }
    }


    @RequestMapping("/updateHotel")
    public  R updateHotel(Hotel hotel){
        int i = hotelService.updateHotel(hotel);
        if (i==1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @PostMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {

        if (file != null) {
            //得到文件名
            String originalFilename = file.getOriginalFilename();

            InputStream inputStream = file.getInputStream();

            String url = OssUtil.uploadFile((FileInputStream) inputStream, originalFilename);

            //把文件 url 写给前端

            return R.ok().data("url", url);
        }
        return R.ok();

    }
}
